﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;

using YT.DB;
using YT.Commons;
using YT.Modules;

namespace YT.XH.NOSE
{
    public partial class frmNSSkinPrickTestHdr : Form
    {

        private DBAccess mdbcon = new DBAccess(false, false);
        private string mstrRecID = string.Empty;
        private string mstrSickRecID = string.Empty;
        private string mstrTableName = string.Empty;
        private FormStatus mFormStatus = new FormStatus();
        private ArrayList mArrayControl = new ArrayList();

        public frmNSSkinPrickTestHdr()
        {
            InitializeComponent();
        }

        private void FillBaseInfo()
        {
            string strSQL = string.Empty;
            strSQL = "select * from mSickPerson where sickRecID='" + mstrSickRecID + "'";
            DataRow drRow = mdbcon.GetDataFirstRow(strSQL);
            if (drRow != null)
            {
                txtSickName.Text = CvtFunc.ToString(drRow["SickName"]);
                txtAddress.Text = CvtFunc.ToString(drRow["Address"]);
                txtPhone.Text = CvtFunc.ToString(drRow["Phone"]) + CvtFunc.ToString(drRow["MobilePhone"]);
                txtRemark.Text = CvtFunc.ToString(drRow["Remark"]);
            }

        }

        private void frmNSSkinPrickTestHdr_Load(object sender, EventArgs e)
        {
            mstrTableName = "NoseSkinPrickTestHdr";
            mstrSickRecID = SysVar.SickRecID;
            FillBaseInfo();
            FillGrid();
            this.Text = "皮试记录表";
            timer1.Start();
        }

        private bool UpdateRemark()
        {
            this.dgvDetail.EndEdit();
            string strSQL = string.Empty;
            string strRecID = string.Empty;
            foreach (DataGridViewRow dgvRow in this.dgvDetail.Rows)
            {
                strRecID = dgvRow.Cells[ColRecID.Index].Value.ToString();
                string strRemark = dgvRow.Cells[ColRemark.Index].Value.ToString();
                strSQL = "update " + mstrTableName + " set Remark ='{0}' where RecID='{1}'";
                strSQL = string.Format(strSQL, strRemark, strRecID);
                mdbcon.ExecuteSql(strSQL);
            }
            return true;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (this.Opacity >= 1)
            {
                timer1.Stop();
            }
            this.Opacity += 0.04;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (UpdateRemark())
                this.Close();
        }

        private void FillGrid()
        {
            this.dgvDetail.Rows.Clear();
            string strSQL = string.Empty;
            strSQL = "select * from " + mstrTableName + " where SickRecID='" + mstrSickRecID + "' ";
            DataTable dtDtl = mdbcon.GetDataTable(strSQL);
            foreach (DataRow drRow in dtDtl.Rows)
            {
                this.dgvDetail.Rows.Add();
                this.dgvDetail.Rows[this.dgvDetail.Rows.Count - 1].Cells[ColRecID.Index].Value = CvtFunc.ToString(drRow["RecID"]);
                this.dgvDetail.Rows[this.dgvDetail.Rows.Count - 1].Cells[ColSeqNo.Index].Value = CvtFunc.ToString(drRow["SeqNo"]);
                this.dgvDetail.Rows[this.dgvDetail.Rows.Count - 1].Cells[ColDate.Index].Value = BaseFunctions.FormatDate(drRow["ExamDate"].ToString(), DateFormatOption.DateFmtYMD);
                this.dgvDetail.Rows[this.dgvDetail.Rows.Count - 1].Cells[ColRemark.Index].Value = CvtFunc.ToString(drRow["Remark"]);
            }
            this.dgvDetail.EndEdit();
        }

        private void dgvDetail_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == ColDtl.Index)
            {
                ShowMtn();
            }
        }

        private void ShowMtn()
        {
            string strSeqNo = string.Empty;
            if (this.dgvDetail.Rows.Count >= 1)
                strSeqNo = CvtFunc.ToString(this.dgvDetail.CurrentRow.Cells[ColSeqNo.Index].Value);

            frmNSSkinPrickTestDetail frmDetail = new frmNSSkinPrickTestDetail(mstrSickRecID, strSeqNo);
            frmDetail.ShowDialog();
            FillGrid();
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            frmNSSkinPrickTestDetail frmDetail = new frmNSSkinPrickTestDetail(mstrSickRecID, "");
            frmDetail.ShowDialog();
            FillGrid();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close(); 
        }


    }
}
